package cn.wenhaha.app.log.service.imp;

import cn.hutool.core.util.StrUtil;
import cn.wenhaha.app.log.bean.vo.ErrorLogVo;
import cn.wenhaha.app.log.service.ErrorLogService;
import cn.wenhaha.common.bean.query.mapping.ErrorLogQuery;
import cn.wenhaha.common.db.bean.SysDataSource;
import cn.wenhaha.common.db.bean.SysSyncError;
import cn.wenhaha.common.exception.ApiMSGException;
import cn.wenhaha.common.utils.PageConvert;
import com.jfinal.plugin.activerecord.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 实现类
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2023-01-20 14:32
 */
@Service
public class ErrorLogServiceImp implements ErrorLogService {

    @Resource
    private SysSyncError errorMapping;

    @Override
    public Page<ErrorLogVo> list(ErrorLogQuery query) {
        Page<SysSyncError> errorLogPage = errorMapping.queryPageList(query);
        return PageConvert.convertPage(errorLogPage, model -> new ErrorLogVo()
                .setId(model.getId())
                .setName(model.getName())
                .setType(model.getType())
                .setDataName(model.getNameData())
                .setUploadName(model.getNameUpload())
                .setRowId(model.getRowId())
                .setCreateTime(model.getCreateTime())
                .setLock(model.getLock())
        );
    }

    @Override
    public SysSyncError detail(String id) {

        return Optional.ofNullable(errorMapping.findById(id))
                .orElseThrow(() -> ApiMSGException.build(9002));
    }

    @Override
    public Boolean lockChange(String id) {
        SysSyncError detail = detail(id);

        String lock = detail.getLock();
        if (StrUtil.equals(lock,"1")){
            detail.setLock("0");
        }else{
            detail.setLock("1");
        }
        return  detail.update();
    }
}
